package com.ftg.learn.chapter08;

/**
 * @author KangJx
 * @date 2021/7/13
 */
public class Gua {


    /**
     * 这是一个青蛙跳井的问题，的解决方案
     * @param wellHeight  井深
     * @param jumpHeight  跳的高度
     * @param down        下滑几米
     * @return
     */
    public int jumpWell(int wellHeight,int jumpHeight,int down){

        if(jumpHeight >= wellHeight){
            return 1;
        }
        if(jumpHeight <= down){
            return 0;
        }
        //次数
        int no = 1;

        //青蛙现在的位置 frogPos
        for(int frogPos = jumpHeight; frogPos < wellHeight;frogPos+=jumpHeight){
            no++;
            frogPos-=down;
        }

        return no;
    }

    public int FrogJumpWell(int wellHeight,int jumpHeight,int down){

        if(jumpHeight >= wellHeight){
            return 1;
        }
        if(jumpHeight <= down){
            return 0;
        }
        int frogPos = jumpHeight;
        int no =1;
        while (frogPos < wellHeight){
            no++;
            frogPos-=down;
            frogPos+=jumpHeight;
        }
        //表达1
        //  while(表达2){
        //      代码块
        //      exp3
        // }
        return no;
    }

    public int FrogdoWhileWell(int wellHeight,int jumpHeight,int down){

        if(jumpHeight >= wellHeight){
            return 1;
        }
        if(jumpHeight <= down){
            return 0;
        }
        int no = 0;
        int frogpostion = 0;
        do{
            if(no !=0){
                frogpostion-=down;
            }
            frogpostion+=jumpHeight;
            no++;
        }while (frogpostion < wellHeight);
        return no;
    }
//    1 8 = 1
//
//    2 7 = 2
//
//    3 6 = 3
//
//    4 5 = 4
//
//    5 5 = 5
//
//

    /**
     * 规律：每一行循环输出的内容为，
     *  1、列数小于行数时，输出列数，否则出行数
     *  2、列数大于最大列数减去行数时，输出列数，否则输出行数
     */
    public void questLast(){
        for(int i = 1;i < 10;i++){
            for(int j = 1,jlen=10;j < jlen;j++){
                System.out.print(i<=jlen>>1?(j<=i?j:(j > jlen-i)?jlen-j:i):(j < jlen+1-i)?j:j > i?jlen-j:jlen-i);
            }
            System.out.println();
        }
    }


    public void exchange(int a,int b){
        System.out.println("没有交换以前的值为");
        System.out.println("a="+a+"\tb="+b);

        int c = a;
        a = b;
        b =c;

        System.out.println("交换以后的值为");
        System.out.println("a="+a+"\tb="+b);
    }

    public void exchange1(int a,int b){
        System.out.println("没有交换以前的值为");
        System.out.println("a="+a+"\tb="+b);

        a = a + b;

        b = a- b;

        a = a - b;

        System.out.println("交换以后的值为");
        System.out.println("a="+a+"\tb="+b);
    }

    /**
     * 。。。 ^
     * @param a
     * @param b
     */
    public void exchange2(int a,int b){
        System.out.println("没有交换以前的值为");
        System.out.println("a="+a+"\tb="+b);

        //不同为1 相同为0
        //a = 13;
        a = a^b;
        //b = 4
        b = a^b;
        //a = 9
        a = a^b;

        System.out.println("交换以后的值为");
        System.out.println("a="+a+"\tb="+b);
    }


    public void questLast1(){
        for(int i = 1;i < 10;i++) {
            for (int j = 1, jlen = 10; j < jlen; j++) {
                if(i <= 5){
                    if( j<=i ){
                        System.out.print(j);
                    }else if(j > jlen-i){
                        System.out.print(jlen - j);
                    }else{
                        System.out.print(i);
                    }
                }else{
                    if(j < jlen+1-i){
                        System.out.print(j);
                    }else if(j > i){
                        System.out.print(jlen-j);
                    }else{
                        System.out.print(jlen-i);
                    }
                }

            }
            System.out.println();
        }
            System.out.println();
        }

    /**
     * 河里有7块石头，一只青蛙站在第一块石头上 ，依次向前跳。当跳到第7块石头时开始向后依次条。
     * 跳到第一块石头上再向前跳。编程计算 跳30下后青蛙站到那块石头上？
     */
    public void testStone(int index){
        int cs = 1;
        int t = 1;
        for(int i = 1;i <= index;i++){
          if(i % 7 ==0){
              cs++;
              t++;
          }
          if(cs % 2 == 0){
              --t;
          }else{
              ++t;
          }
        }
        System.out.println(t);
    }

    public int testStone1(int index){
        //次数
        int cs = 1;
        //青蛙所在的石头编号
        int t = 1;
        //总循环次数
        for(int i = 1;i <= index;i++){
            //7次以后要考虑 增 减问题
            if(i % 7 ==0){
                cs++;
                t++;
            }
            //周期为奇数+1 为偶数-1
            t+=cs%2 ==0?-1:+1;
        }
        return t;
    }

    
}
